package com.lottery.game.runner;

import com.lottery.game.core.modules.service.app.GameUserService;
import com.lottery.game.core.modules.service.redis.RedisService;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import lombok.extern.slf4j.Slf4j;

/**
 * WebSocketService.sendOnlineUser 替换该功能
 * @author Administrator
 *
 */
@Slf4j
//@Component
@Deprecated
public class UserOnlineRunner implements ApplicationRunner {

	@Autowired
	private RedisService redisFeignClient;

	@Autowired
	private GameUserService gameUserService;

	@Override
	public void run(ApplicationArguments args) throws Exception {

		log.info("开始更新在线人数。。");

		// 这个需要做修改
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();

		TimerTask task = new TimerTask() {

			@Override
			public void run() {
            	try {
            		// 获取所有USERID开头的记录
            		// USERID开头的记录是在用户登录时， 调用UserLoginService.getGameUserBynamepwd生成的
            		// key: USERIDxxxx  value: 登录的时间
            		Set<String> set = redisFeignClient.keys("USERID*");

            		set.stream().forEach(userIdKey -> {
            			long currentTime = System.currentTimeMillis();//当前时间
            			String val = redisFeignClient.get(userIdKey);
            			long loginTime = Long.parseLong(val); //用户的时间

            			// 登录时间和当前时间相差的时间
            			long time = currentTime - loginTime;

            			// 相差时间超过600秒
            			if(time >= 600000) {//3600000

            				Map<String, Object> map=new HashMap<>();

            				String userId = userIdKey.substring(6, userIdKey.length());

            				map.put("userOnlineStatus","0");
            				map.put("userId", userId);

            				list.add(map);

            				// 下面2句为什么从redis删除
            				redisFeignClient.delete(userIdKey);
            				redisFeignClient.delete("UUID" + userId);
            			}
            		});

	            	if(!CollectionUtils.isEmpty(list)) {
	            		int num = gameUserService.updateBatchUserOnlineStatus(list);
	            		if(num>0) {
	            			list.clear();
	            		}
	            	}
            	} catch (Exception e) {
            		log.error(e.getMessage(), e);
            	}
			}
		};

		Timer timer = new Timer();
		timer.scheduleAtFixedRate(task, 0, 3000);
	}
	//Set item=jedis.keys("USERID*");


	/* Map<String, Object> map=CacheUtil.getInstance().get();
	                for (Map.Entry<String, Object> entry : map.entrySet()) {
						long newDate=System.currentTimeMillis();
						long userDate=(long) entry.getValue();
						if(newDate-userDate>3600000) {//3600000 一个小时   // 1800000 半个小时
							try {
								CacheUtil.getInstance().del(entry.getKey());//删除缓存的数据
								redisUtil.del(entry.getKey());//删除redis缓存里面的数据
								String key=entry.getKey();
								logger.info("截取的key:"+key.substring(4, key.length()));
								gameUserService.updateUserOnlineStatus("0", key.substring(4, key.length()));//修改用户的状态
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}

					}*/


}

